package com.yadang.infra.service;

import com.yadang.infra.bean.Locations;
import com.yadang.infra.mapper.LocationsMapper;
import com.yadang.infra.tools.SnowFlakeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class LocationsService {
    private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    @Autowired
    private LocationsMapper locationsMapper;

    public List<Locations> selectLocations(String name, String parentId, String level) {
        List<Locations> locations = locationsMapper.selectAll();
        return locations;
    }

    public List<String> selectAllProvice() {
        Locations locations = new Locations();
        locations.setLevel("province");
        List<Locations> provinceList = locationsMapper.selectProvinceByBean(locations);
        Set<String> collect = provinceList.stream().map(Locations::getName).collect(Collectors.toSet());
        return collect.stream().collect(Collectors.toList());
    }

    /**
     * @param provinceName 省份名
     * @param cityName     市名
     * @param countyName   县名
     * @param level        省市县标识 1-省；2-市；3县
     * @return
     */
    public String insert(String provinceName, String cityName, String countyName, String level) {
        List<String> levelList = Arrays.asList("1", "2", "3");
        if (!levelList.contains(level)) {
            return "输入标识不正确：" + level;
        }

        if ("1".equals(level)) {
            if (!StringUtils.hasText(provinceName)) {
                return "省不能为空！";
            }
            Locations locations = new Locations();
            locations.setName(provinceName);
            List<Locations> provinceList = locationsMapper.selectProvinceByBean(locations);
            if (provinceList.size() > 0) {
                return provinceName + "已经存在";
            }
            String id = SnowFlakeUtil.getId();
            locations.setId(id);
            locations.setLevel(level);
            locationsMapper.insert(locations);
            return "插入成功！";
        }
        // 插入的是市
        if ("2".equals(level)) {
            if (!StringUtils.hasText(provinceName)) {
                return "省不能为空！";
            }
            if (!StringUtils.hasText(cityName)) {
                return "市不能为空！";
            }
            Locations locations = new Locations();
            locations.setName(provinceName);
            List<Locations> provinceList = locationsMapper.selectProvinceByBean(locations);
            // 校验该市所对应的省是否存在
            if (provinceList.size() < 1) {
                return provinceName + "不存在,请先增加省份";
            }
            // 校验插入的市是否已经存在
            locations.setName(cityName);
            List<Locations> cityList = locationsMapper.selectProvinceByBean(locations);
            if (cityList.size() > 0) {
                return cityName + "已经存在！";
            }
            String provinceId = provinceList.get(0).getId();
            String id = SnowFlakeUtil.getId();
            locations.setId(id);
            locations.setName(cityName);
            locations.setLevel(level);
            locations.setParentId(provinceId);
            locationsMapper.insert(locations);
            return "插入成功！";
        }
        // 插入的是县
        if ("3".equals(level)) {
            if (!StringUtils.hasText(provinceName)) {
                return "省不能为空！";
            }
            if (!StringUtils.hasText(cityName)) {
                return "市不能为空！";
            }
            if (!StringUtils.hasText(countyName)) {
                return "市不能为空！";
            }
            Locations locations = new Locations();
            locations.setName(cityName);
            List<Locations> cityList = locationsMapper.selectProvinceByBean(locations);
            if (cityList.size() < 1) {
                return cityName + "不存在,请先增加市！";
            }
            locations.setName(countyName);
            List<Locations> countyList = locationsMapper.selectProvinceByBean(locations);
            if (countyList.size() > 0) {
                return countyName + "已经存在";
            }
            String cityId = cityList.get(0).getId();
            String id = SnowFlakeUtil.getId();
            locations.setId(id);
            locations.setName(countyName);
            locations.setLevel(level);
            locations.setParentId(cityId);
            locationsMapper.insert(locations);
            return "插入成功！";
        }

        /*Locations locations = new Locations();
        locations.setName(provinceName);
        List<Locations> provinceList = locationsMapper.selectProvinceByBean(locations);
        if (provinceList.size() > 0) {
            return provinceName + "已经存在";
        }

        String id = SnowFlakeUtil.getId();
        locations.setId(id);
        locations.setLevel(level);
        int insert = locationsMapper.insert(locations);
        return "插入成功！";*/
        return "未知异常！" + provinceName + " " + cityName + " " + countyName;
    }
}
